{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2024-04-01 06:17:22,626 [warning] Failed resolving version info. Ignoring and using defaults\n",
      "> 2024-04-01 06:17:25,574 [warning] Server or client version is unstable. Assuming compatible: {\"client_version\":\"0.0.0+unstable\",\"server_version\":\"1.7.0-rc6\"}\n"
     ]
    }
   ],
   "source": [
    "import mlrun\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2024-04-01 06:17:25,768 [info] Loading project from path: {\"path\":\"./\",\"project_name\":\"model-serving\"}\n",
      "> 2024-04-01 06:17:41,171 [info] Project loaded successfully: {\"path\":\"./\",\"project_name\":\"model-serving\",\"stored_in_db\":true}\n"
     ]
    }
   ],
   "source": [
    "project_name = \"model-serving\"\n",
    "project = mlrun.get_or_create_project(name=project_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mlrun: start-code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mlrun"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocess_seconds = 0.1\n",
    "prediction_seconds = 0.1\n",
    "postprocess_seconds = 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ClassifierModel(mlrun.serving.V2ModelServer):\n",
    "    def load(self):\n",
    "        pass\n",
    "\n",
    "    def predict(self, body: dict):\n",
    "        # time.sleep(prediction_seconds)\n",
    "        return []\n",
    "\n",
    "\n",
    "#     def preprocess(self, request: dict, operation) -> dict:\n",
    "#         time.sleep(preprocess_seconds)\n",
    "#         return request\n",
    "\n",
    "#     def postprocess(self, request: dict):\n",
    "#         time.sleep(postprocess_seconds)\n",
    "#         return request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mlrun: end-code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "fn = mlrun.code_to_function(\n",
    "    \"v2-model-server\",\n",
    "    kind=\"serving\",\n",
    "    code_output=\".\",\n",
    "    image=\"mlrun/mlrun:1.7.0-rc6\",\n",
    ")\n",
    "fn.spec.min_replicas = 1\n",
    "fn.spec.max_replicas = 1\n",
    "fn.spec.default_class = \"ClassifierModel\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": ""
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mlrun.serving.states.TaskStep at 0x7f34e822bfa0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn.add_model(\"mymodel\", model_path=\"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> 2024-04-01 06:17:45,611 [info] Starting remote function deploy\n",
      "2024-04-01 06:17:45  (info) Deploying function\n",
      "2024-04-01 06:17:45  (info) Building\n",
      "2024-04-01 06:17:45  (info) Staging files and preparing base images\n",
      "2024-04-01 06:17:45  (warn) Using user provided base image, runtime interpreter version is provided by the base image\n",
      "2024-04-01 06:17:45  (info) Building processor image\n"
     ]
    }
   ],
   "source": [
    "fn.apply(mlrun.mount_v3io())\n",
    "invoke_url = fn.deploy(project=project_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "!wget https://github.com/gtopper/postto/releases/download/v1.2.1/postto_v1.2.1_linux_amd64"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "!chmod u+x postto_v1.2.1_linux_amd64"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"BENCH_INVOKE_URL\"] = invoke_url\n",
    "os.environ[\"BENCH_CONCURRENT_REQUESTS\"] = \"16\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": "!for i in {1..50000}; do echo '{\"inputs\": []}'; done | ./postto_v1.2.1_linux_amd64 -num-concurrent-requests $BENCH_CONCURRENT_REQUESTS $BENCH_INVOKE_URL"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mlrun-base",
   "language": "python",
   "name": "conda-env-mlrun-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
